********************************************************************************************
* FIGURE COMPARING FIRST STAGE T-STATISTIC WHEN USING DIFFERENT SETTLEMENT SIZE THRESHOLDS *
********************************************************************************************
* Using data at the vendor level, run first-stage regressions when changing the settlement size threshold criteria:
use "$temporales\Bcom_976total_2022.dta",clear

* Construct formal enforcement at the individual level:
	* Generate dummies indicating sanctions perceptions (by fraction):
	gen enf_form1=inlist(indenf_nopaga,4,5,6,7) 
	gen enf_form2=inlist(indenf_estafa,4,5,6,7) 
	gen enf_form3=inlist(indenf_limpia,4,5,6,7) 

	* Compute share of fractions where vendor expects sanctions
	egen enf_form_ind=rmean(enf_form1 enf_form2 enf_form3)

* Get the share of neighbors that come from the same market from the market-level data:
	merge m:1 codmerc using "$temporales\Bmdo_90total_2022.dta", nogen keepusing(pvecin_orig) 
	drop vecFIN

	forval threshold=20(20)200{
		preserve

		* Generate new Social Ties variable:
		gen vecFIN=((pvecin_orig>.5) & (tama_urb<=`threshold'))
		
		* Run first-stage regression:
		reg $enf $ties $bp07 $mdo $ven i.zona20, $vce
		parmest, fast label
		gen threshold=`threshold'
		
		* Save the results in a temporary dataset:
		tempfile firststa_`threshold'
		save `firststa_`threshold'', replace
		
		restore
	}

* Append all regression results:
	clear all
	forval threshold=20(20)200{
	append using `firststa_`threshold''
	}

* Plot t-statitic against threshold:
	keep if parm=="vecFIN"
	twoway (scatter t threshold, mcolor(black)), xlab(0(20)200) ylabel(3(.5)5, angle(horizontal)) ytitle(`"First-stage t-statistic"') xtitle(`"Settlement size threhold (ha)"') graphregion(fcolor(white) lcolor(none)) xline(80, lpattern(dash) lcolor(black))
	graph export "$figuras\figure2.pdf", replace
	
	

********************************************************************************
* TABLE 4: ENFORCEMENT AND INFRASTRUCTURE GROWTH (+ TAB A1)					   *
********************************************************************************
use "$temporales\Bcom_976total_2022.dta",clear

* Construct formal enforcement at the individual level:

* Generate dummies indicating sanctions perceptions (by fraction)
	gen enf_form1=inlist(indenf_nopaga,4,5,6,7) 
	gen enf_form2=inlist(indenf_estafa,4,5,6,7) 
	gen enf_form3=inlist(indenf_limpia,4,5,6,7) 
	gen enf_form1S=inlist(indenf_nopaga,5,6,7) 
	gen enf_form2S=inlist(indenf_estafa,5,6,7) 
	gen enf_form3S=inlist(indenf_limpia,5,6,7) 
	gen enf_soc1=inlist(indenf_nopaga,2,3) 
	gen enf_soc2=inlist(indenf_estafa,2,3) 
	gen enf_soc3=inlist(indenf_limpia,2,3)

* Compute share of fractions where vendor expects sanctions:
	egen enf_form_ind=rmean(enf_form1 enf_form2 enf_form3)
	egen enf_formS_ind=rmean(enf_form1S enf_form2S enf_form3S)
	egen enf_soc_ind=rmean(enf_soc1 enf_soc2 enf_soc3)

* Define label of variables:
	la var $ties "Social ties among market founders (1-0)"
	la var $enf "Formal Enforcement"

	la var enf_form "Formal Enforcement" 
	la var puestos "Number of stalls in market"
	la var antiguedad "Age of market"
	la var p_funda "Percent of market stalls owned by founders (CENAMA)"
	la var p_verdura "Percent of stalls that sell vegetables (CENAMA)"
	la var p_fruta "Percent of stalls that sell fruit (CENAMA)"
	la var p_carne "Percent of stalls that sell meat (CENAMA)"
	la var p_ave "Percent of stalls that sell chicken (CENAMA)"
	la var p_pes "Percent of stalls that sell fish (CENAMA)"
	la var p_abar "Percent of stalls that sell dried goods (CENAMA)"
	la var indmdos17 "Index of number/distance of traditional markets around market (2017)"
	la var indmdos07 "Index of number/distance of traditional markets around market (2007)"
	la var indsuper "Index of number/distance of supermarkets around market"
	la var Bpcomplex_2007 "Market infrastructure stock 2007"
	la var tienece "Market currently collecting extraordinary dues"
	la var Pollo "Sells poultry"
	la var Carne "Sells meat"
	la var Frutas "Sells fruits"
	la var Verduras "Sells vegetables"
	la var Abarrotes "Sells dried goods"
	la var edu_nivel "Education level"
	la var edad "Age"
	la var fundador "Founder"
	la var otro_puesto "Owns another stall"
	la var m2_puesto "Stall size (m2)"
	la var hombre "Male"
	la var nac_fuera "Born outside of Metropolitan Lima"
	 
* OLS:
	eststo: xi:  ivreg2 $bp $ties $bp07 i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 $enf $mdo $ven i.zona20, $vce

* Three-panel table set-up:
	local preheadA  "\begin{tabular}{l*{4}{c}} \hline" 
	local postheadA "\midrule \multicolumn{5}{l}{\textbf{Panel A: OLS – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postheadB "\midrule \multicolumn{5}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement and Social Ties}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Broad Formal Enforcement}} \\ "
	local postheadC "\midrule \multicolumn{5}{l}{\textbf{Panel C: IV Second Stage – Public Goods and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postfootC "\bottomrule \end{tabular}" 

* Export OLS results:
esttab using "$tablas\tabla4_vendor_comp.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
	star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
	nomtitle keep($ties $enf )
	

* IV - First stage:
	eststo: xi: ivreg2 $bp ($enf=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(tab4_1stnoc)
	estimates restore tab4_1stnoc*
	eststo tab4_1stnoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stnoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(tab4_1stmdoc)
	estimates restore tab4_1stmdoc*
	eststo tab4_1stmdoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab4_1stmdoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab4_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab4_1stvenc)
	estimates restore tab4_1stvenc*
	eststo tab4_1stvenc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1stvenc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1stvenc //Save First stage R2

	esttab  tab4_1stnoc tab4_1stmdoc tab4_1stvenc using "$tablas\tabla4_vendor_comp.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep($ties) extracols(1) posthead(`postheadB') 
	eststo clear 

* IV - Second stage:
	eststo iv_nocon : xi:  ivregress 2sls $bp ($enf=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon

	eststo iv_mdo : xi:  ivregress 2sls $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo 

	eststo iv_ven : xi: ivregress 2sls $bp ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven

* Export only main variables:
	esttab iv_nocon iv_mdo iv_ven using "$tablas\tabla4_vendor_comp.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep($enf) extracols(1) posthead(`postheadC') postfoot(`postfootC')

	* TABLE A1 (FULL TABLE WITH CONTROLS):
	esttab iv_nocon iv_mdo iv_ven using "$tablas\tablaA1_vendor_comp.tex", ///
    replace label b(%9.3f) se(%9.3f) booktabs ///
    star(* 0.1 ** 0.05 *** 0.01) nomtitles ///
    stats(N zone_fe, fmt(%5.3g) ///
    labels("Observations" "Zone fixed effects" "Market controls" "Vendor controls")) ///
		   keep($enf $bp07 $mdo $ven)

	eststo clear
		   
********************************************************************************
* TABLE 5: ENFORCEMENT AND OTHER COLLECTIVE ACTION MEASURES					   *
********************************************************************************
merge m:1 codmerc using "$temporales\Bmdo_90total_2022.dta", nogen keepusing(nserv) //Get the share of services dependent variable

* OLS:
eststo: xi:  ivreg2 $nser $enf $bp07 i.zona20, $vce

eststo: xi:  ivreg2 $nser $enf $bp07 $mdo i.zona20, $vce

eststo: xi:  ivreg2 $nser $enf $bp07 $mdo $ven i.zona20, $vce

eststo: xi:  ivreg2 $cord $enf tienece $bp07 i.zona20, $vce

eststo: xi:  ivreg2 $cord $enf tienece $bp07 $mdo i.zona20, $vce

eststo: xi:  ivreg2 $cord $enf tienece $bp07 $mdo $ven i.zona20, $vce
	
* Two-panel table set-up:
	local preheadA  "\begin{tabular}{l*{6}{c}} \hline  &\multicolumn{3}{c}{Dep var: Percentage of five services }&\multicolumn{3}{c}{Dep var: Percentage of vendors who } \\ &\multicolumn{3}{c}{ provided by market}&\multicolumn{3}{c}{regularly pay their ordinary dues} \\ \cmidrule(lr){2-4}\cmidrule(lr){5-7}"
	local postheadA "\midrule \multicolumn{7}{l}{\textbf{Panel A: OLS – Market Collective Action and Formal Enforcement}} \\"
	local postheadB "\midrule \multicolumn{7}{l}{\textbf{Panel B: IV Second Stage – Market Collective Action and Formal Enforcement}} \\"
	local postfootB "\bottomrule \end{tabular}"

* Export OLS results:
	esttab using "${tablas}\tabla5_vendor_comp.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		nomtitle keep( $enf )

	eststo clear
	
* IV - Second stage:
	eststo iv_nocon_ser : xi:  ivregress 2sls $nser ($enf=$ties) $bp07 i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon_ser
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon_ser
	xi: ivreg2 $nser ($enf=$ties) $bp07 i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': iv_nocon_ser // Save Kleibergen-Papp F statistic


	eststo iv_mdo_ser : xi:  ivregress 2sls $nser ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo_ser 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo_ser
	xi: ivreg2 $nser ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': iv_mdo_ser // Save Kleibergen-Papp F statistic 

	eststo iv_ven_ser : xi: ivregress 2sls $nser ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven_ser 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven_ser
	xi: ivreg2 $nser ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': iv_ven_ser // Save Kleibergen-Papp F statistic 

	eststo iv_nocon_cord : xi:  ivregress 2sls $cord ($enf=$ties) tienece $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon_cord
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon_cord
	xi: ivreg2 $cord ($enf=$ties) tienece $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': iv_nocon_cord // Save Kleibergen-Papp F statistic 

	eststo iv_mdo_cord : xi:  ivregress 2sls $cord ($enf=$ties) $bp07 tienece $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo_cord 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo_cord 
	xi: ivreg2 $cord ($enf=$ties) $bp07 tienece $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': iv_mdo_cord // Save Kleibergen-Papp F statistic

	eststo iv_ven_cord : xi: ivregress 2sls $cord ($enf=$ties) $bp07 tienece $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven_cord 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven_cord
	xi: ivreg2 $cord ($enf=$ties) $bp07 tienece $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': iv_ven_cord  // Save Kleibergen-Papp F statistic

* Export results:
	esttab using "${tablas}/tabla5_vendor_comp.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower KP_F  N r2 zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" "Kleibergen-Papp F statistic" Observations "R-squared" "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep($enf) posthead(`postheadB') postfoot(`postfootB')

	eststo clear
	estimates clear 

********************************************************************************
* TABLE 6: STRICT BOARD ENFORCEMENT / ALTERNATIVE MEASURES OF ENFORCEMENT	   *
********************************************************************************
*Generate formal enforcement variable but only for no paga (Non payment) and no limpia (Fails to maintain stall clean):
	* Compute share of vendors who expect sanctions for collective infractions
	bys codmerc: egen enf_form1_market=mean(enf_form1)
	bys codmerc: egen enf_form3_market=mean(enf_form3)
	egen enf_form_coll=rmean(enf_form1_market enf_form3_market)
	la var enf_form_coll "Formal Enforcement - Collective Infractions"
	la var $enf2 "Strict Formal Enforcement"

* OLS:
	gen enf_form_alt=enf_form_coll

* Collective Infractions:
	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt $mdo $ven i.zona20, $vce

* Strict enforcement:
	replace enf_form_alt=$enf2

	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $bp $bp07 enf_form_alt $mdo $ven i.zona20, $vce

* Three-panel table set-up:
	local preheadA  "\begin{tabular}{l*{6}{c}} \hline"
	local postheadA "\midrule \multicolumn{7}{l}{\textbf{Panel A: OLS – Public Goods and Formal Enforcement Alternatives}} \\  \multicolumn{7}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postheadB "\midrule \multicolumn{7}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement Alternatives and Social Ties}} \\  \multicolumn{7}{l}{\textit{Dep. var.: Formal Enforcement Alternatives (indicated in the heading)}} \\ "
	local postheadC "\midrule \multicolumn{7}{l}{\textbf{Panel C: IV Second Stage – Public Goods and Formal Enforcement Alternatives}} \\  \multicolumn{7}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postfootC "\bottomrule \end{tabular}" 

* Export OLS results
	esttab using "${tablas}/tabla6_strictboard.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		mgroups( "Formal Enforcement (Collective Infractions)" "Formal Enforcement (Strict)", pattern(1 0 0 1 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}) )  ///
		nomtitle keep( enf_form_alt ) coeflabels(enf_form_alt "Formal Enforcement Alternative (in column heading)")
		
* IV - First stage:
	* Collective infractions:
	replace enf_form_alt=enf_form_coll

	eststo: xi: ivreg2 $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(coll_1stnoc)
	estimates restore coll_1stnoc*
	eststo coll_1stnoc
	xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': coll_1stnoc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': coll_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(coll_1stmdoc)
	estimates restore coll_1stmdoc*
	eststo coll_1stmdoc
	xi:  ivreg2 enf_form_alt (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': coll_1stmdoc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': coll_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(coll_1stvenc)
	estimates restore coll_1stvenc*
	eststo coll_1stvenc
	xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': coll_1stvenc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': coll_1stvenc //Save First stage R2

	* Strict enforcement 
	replace enf_form_alt=$enf2

	eststo: xi: ivreg2 $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(stri_1stnoc)
	estimates restore stri_1stnoc*
	eststo stri_1stnoc
	xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': stri_1stnoc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': stri_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(stri_1stmdoc)
	estimates restore stri_1stmdoc*
	eststo stri_1stmdoc
	xi:  ivreg2 enf_form_alt (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': stri_1stmdoc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': stri_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(stri_1stvenc)
	estimates restore stri_1stvenc*
	eststo stri_1stvenc
	xi:  ivreg2 $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': stri_1stvenc // Save Kleibergen-Papp F statistic
	reg enf_form_alt $ties $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': stri_1stvenc //Save First stage R2

	esttab  coll_1stnoc coll_1stmdoc coll_1stvenc stri_1stnoc stri_1stmdoc stri_1stvenc using "${tablas}/tabla6_strictboard.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep($ties) posthead(`postheadB') 
	eststo clear 

* IV - Second stage:
	* Collective infractions:
	replace enf_form_alt=enf_form_coll

	eststo coll_nocon : xi:  ivregress 2sls $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": coll_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": coll_nocon

	eststo coll_mdo : xi:  ivregress 2sls $bp (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": coll_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": coll_mdo 

	eststo coll_ven : xi: ivregress 2sls $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": coll_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": coll_ven

	* Strict enforcement:
	replace enf_form_alt=$enf2

	eststo stri_nocon : xi:  ivregress 2sls $bp (enf_form_alt=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": stri_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": stri_nocon

	eststo stri_mdo : xi:  ivregress 2sls $bp (enf_form_alt=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": stri_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": stri_mdo 

	eststo stri_ven : xi: ivregress 2sls $bp (enf_form_alt=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": stri_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": stri_ven

* Export only main variables:
	esttab coll_nocon coll_mdo coll_ven stri_nocon stri_mdo stri_ven using "${tablas}/tabla6_strictboard.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep( enf_form_alt ) coeflabels(enf_form_alt "Formal Enforcement Alternative (in column heading)") posthead(`postheadC') postfoot(`postfootC')

	eststo clear
	estimates clear

********************************************************************************
* TABLE 7 - ENFORCEMENT AND INFRASTRUCTURE, ROBUSTNESS TO SETTLEMENT SIZE	   *
********************************************************************************
merge m:1 codmerc using "$temporales\Bmdo_90total_2022.dta", nogen keepusing(pvecin_orig) 

* Generate new Social Ties variable with only the criteria of the >51% of members coming from the same settlement:
	gen vecFIN_nosize=(pvecin_orig>.5)
	la var vecFIN_nosize "Social ties without settlement size criterion"

* Generate new social ties variables using thresholds of 105 and 200 hectares for the origin settlement size criterion:
	gen vecFIN_105=((pvecin_orig>.5) & (tama_urb<=105))
	gen vecFIN_200=((pvecin_orig>.5) & (tama_urb<=200))
	la var vecFIN_105 "Social ties using 105 ha. as size criterion"
	la var vecFIN_200 "Social ties using 200 ha. as size criterion"

* Reduced Form:

	* Eliminating the size criterion overall:
	eststo: xi:  ivreg2 $bp vecFIN_nosize $bp07 $mdo $ven i.zona20, $vce

	* Establishing the size threshold on 105 hectares (instead of 80):
	eststo: xi:  ivreg2 $bp vecFIN_105 $bp07 $mdo $ven i.zona20, $vce

	*Establishing the size threshold on 200 hectares (instead of 80):
	eststo: xi:  ivreg2 $bp vecFIN_200 $bp07 $mdo $ven i.zona20, $vce

* Export results:

* Three-panel table set-up:
	local preheadA  "\begin{tabular}{l*{3}{c}} \hline"
	local postheadA "\midrule \multicolumn{4}{l}{\textbf{Panel A: OLS – Public Goods and Social Ties}} \\  \multicolumn{4}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postheadB "\midrule \multicolumn{4}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement and Social Ties}} \\  \multicolumn{4}{l}{\textit{Dep. var.: Formal Enforcement}} \\ "
	local postheadC "\midrule \multicolumn{4}{l}{\textbf{Panel C: IV Second Stage – Public Goods and Formal Enforcement}} \\  \multicolumn{4}{l}{\textit{Dep. var.: Change in Market Infrastructure (Market Investment) Index, 2007–2014}} \\ "
	local postfootC "\bottomrule \end{tabular}"

* Export OLS results:
	esttab using "${tablas}/tabla7_settlement.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		nomtitle keep(vecFIN*) order(vecFIN*)

	eststo clear

* IV - First stages:

	* Without size criterion:
	eststo: xi:  ivreg2 $bp ($enf= vecFIN_nosize ) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab4_1stnosz)
	estimates restore tab4_1stnosz*
	eststo tab4_1stnosz
	xi:  ivreg2 $bp ($enf= vecFIN_nosize) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1stnosz // Save Kleibergen-Papp F statistic
	reg $enf vecFIN_nosize $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1stnosz //Save First stage R2

	* 105 has as theshold for the size criterion:
	eststo: xi:  ivreg2 $bp ($enf= vecFIN_105 ) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab4_1st105)
	estimates restore tab4_1st105*
	eststo tab4_1st105
	xi:  ivreg2 $bp ($enf= vecFIN_105) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1st105 // Save Kleibergen-Papp F statistic
	reg $enf vecFIN_105 $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1st105 //Save First stage R2

	* 200 has as theshold for the size criterion:
	eststo: xi:  ivreg2 $bp ($enf= vecFIN_200 ) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab4_1st200)
	estimates restore tab4_1st200*
	eststo tab4_1st200
	xi:  ivreg2 $bp ($enf= vecFIN_200) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab4_1st200 // Save Kleibergen-Papp F statistic
	reg $enf vecFIN_200 $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab4_1st200 //Save First stage R2

	* Export:
	esttab  tab4_1stnosz tab4_1st105 tab4_1st200 using "${tablas}/tabla7_settlement.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep(vecFIN*) order(vecFIN*) posthead(`postheadB')
	eststo clear 

* IV - Second stage:

	* Without size criterion
	eststo iv_nosize : xi:  ivregress 2sls $bp ($enf= vecFIN_nosize ) $bp07  $mdo $ven i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nosize
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nosize

	* 105 has as theshold for the size criterion:
	eststo iv_105 : xi:  ivregress 2sls $bp ($enf= vecFIN_105 ) $bp07  $mdo $ven i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_105 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_105

	* 200 has as theshold for the size criterion:
	eststo iv_200 : xi: ivregress 2sls $bp ($enf= vecFIN_200 ) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_200 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_200

	* Export only main variables:
	esttab iv_nosize iv_105 iv_200 using "${tablas}/tabla7_settlement.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep($enf) posthead(`postheadC') postfoot(`postfootC')

	eststo clear
	estimates clear

********************************************************************************
* TABLE 8 - ENFORCEMENT & GROWTH IN NUMBER OF CUSTOMERS, 2007–2017 (OLS,IV)    *
********************************************************************************

* OLS:
	eststo: xi:  ivreg2 $cli $ties $bp07 i.zona20, $vce

	eststo: xi:  ivreg2 $cli $bp07 $enf i.zona20, $vce

	eststo: xi:  ivreg2 $cli $bp07 $enf $mdo i.zona20, $vce

	eststo: xi:  ivreg2 $cli $bp07 $enf $mdo $ven i.zona20, $vce

* Three-panel table set-up
	local preheadA  "\begin{tabular}{l*{4}{c}} \hline"
	local postheadA "\midrule \multicolumn{5}{l}{\textbf{Panel A: OLS – Growth in Number of Customers and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Difference in log (number of customers), 2017–2007}} \\ "
	local postheadB "\midrule \multicolumn{5}{l}{\textbf{Panel B: IV First Stage – Formal Enforcement and Social Ties}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Formal Enforcement}} \\ "
	local postheadC "\midrule \multicolumn{5}{l}{\textbf{Panel C: IV Second Stage – Growth in Number of Customers and Formal Enforcement}} \\  \multicolumn{5}{l}{\textit{Dep. var.: Difference in log (number of customers), 2017–2007}} \\ "
	local postfootC "\bottomrule \end{tabular}"

* Export OLS results:
	esttab using "${tablas}/tabla8_vendor_comp.tex", replace fragment booktabs  prehead(`preheadA') posthead(`postheadA') label b(%9.3f) se(%9.3f) nocon ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(N r2, fmt(%5.2f) labels(Observations "R-squared")) ///
		nomtitle keep($ties $enf )
	
* IV - First stage:
	eststo: xi: ivreg2 $cli ($enf=$ties) $bp07 i.zona20, $vce first savefirst savefprefix(tab8_1stnoc)
	estimates restore tab8_1stnoc*
	eststo tab8_1stnoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab8_1stnoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab8_1stnoc //Save First stage R2

	eststo: xi:  ivreg2 $cli ($enf=$ties) $bp07 $mdo i.zona20, $vce first savefirst savefprefix(tab8_1stmdoc)
	estimates restore tab8_1stmdoc*
	eststo tab8_1stmdoc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd scalar KP_F=`e(rkf)': tab8_1stmdoc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo i.zona20, $vce
	estadd scalar r2_f=`e(r2)': tab8_1stmdoc //Save First stage R2

	eststo: xi:  ivreg2 $cli ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce first savefirst savefprefix(tab8_1stvenc)
	estimates restore tab8_1stvenc*
	eststo tab8_1stvenc
	xi:  ivreg2 $bp ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd scalar KP_F=`e(rkf)': tab8_1stvenc // Save Kleibergen-Papp F statistic
	reg $enf $ties $bp07 $mdo $ven i.zona20,$vce
	estadd scalar r2_f=`e(r2)': tab8_1stvenc //Save First stage R2

	* Export:
	esttab  tab8_1stnoc tab8_1stmdoc tab8_1stvenc using "${tablas}/tabla8_vendor_comp.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(KP_F N r2_f, fmt(%5.2f) labels( "Kleibergen-Papp F statistic" Observations "R-squared")) nonumber ///
		nomtitle keep($ties) extracols(1) posthead(`postheadB') 
	eststo clear 

* IV - Second stage:
	eststo iv_nocon : xi:  ivregress 2sls $cli ($enf=$ties) $bp07 i.zona20, $vce
	estat weakrobust, ci
	estadd local zone_fe "YES"
	estadd local market_ctls "NO"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_nocon
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_nocon

	eststo iv_mdo : xi:  ivregress 2sls $cli ($enf=$ties) $bp07 $mdo i.zona20, $vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "NO"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_mdo 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_mdo 

	eststo iv_ven : xi: ivregress 2sls $cli ($enf=$ties) $bp07 $mdo $ven i.zona20,$vce
	estadd local zone_fe "YES"
	estadd local market_ctls "YES"
	estadd local vendor_ctls "YES"
	estat weakrobust, ci
	matrix D= r(table)
	mat list D
	local lower= D[2,1]
	local upper= D[3,1]
	local lower		:di %5.3f `lower'
	estadd local lower "`lower'": iv_ven 
	local upper		:di %5.3f `upper'
	estadd local upper "`upper'": iv_ven

	* Export only main variables:
	esttab iv_nocon iv_mdo iv_ven using "${tablas}/tabla8_vendor_comp.tex", append fragment label b(%9.3f) se(%9.3f) nocon booktabs ///
		star(* 0.1 ** 0.05 *** 0.01) nonotes stats(upper lower N zone_fe market_ctls vendor_ctls , fmt(%5.3g) labels( " Upper A-R 95\% CI" " Lower A-R 95\% CI" Observations "Zone fixed effects" "Market controls" "Vendor controls")) nonumber ///
		nomtitle keep($enf) extracols(1) posthead(`postheadC') postfoot(`postfootC')

	eststo clear
	estimates clear
	
* End of 2. analyses.do